package guda.house.web.action.bill;

import guda.grape.autogen.common.page.BizResult;
import guda.grape.util.RequestUtil;
import guda.house.biz.BillElecBiz;
import guda.house.biz.ExtraFeeBiz;
import guda.house.biz.OwnerBiz;
import guda.house.biz.beans.vo.JsonResult;
import guda.house.biz.form.ExtraFeeEditForm;
import guda.house.biz.form.ExtraFeeForm;
import guda.house.biz.form.bill.BillElecEditForm;
import guda.house.biz.form.bill.BillElecForm;
import guda.house.biz.query.BillElecQuery;
import guda.house.biz.query.ExtraFeeQuery;
import guda.house.biz.util.ErrorCode;
import guda.house.dal.dao.BillElecDAO;
import guda.house.dal.dataobject.*;
import guda.house.web.helper.ResponseHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Map;

/**
 * Created by well on 16/1/20.
 */
@Controller
@RequestMapping(value = "bill/extra")
public class ExtraFeeAction {

    @Autowired
    private ExtraFeeBiz extraFeeBiz;


    @RequestMapping(value = "/list.htm", method = RequestMethod.GET)
    public String list(HttpServletRequest request, ModelMap modelMap) {
        int pageId = RequestUtil.getInt(request, "pageNo");
        int pageSize = RequestUtil.getInt(request, "pageSize");
        ExtraFeeQuery extraFeeQuery = new ExtraFeeQuery();
        extraFeeQuery.setPageNo(pageId);
        extraFeeQuery.setPageSize(pageSize);
        modelMap.put("query", extraFeeQuery);
        BizResult bizResult = extraFeeBiz.list(extraFeeQuery);
        if (bizResult.success) {
            modelMap.putAll(bizResult.data);
            return "bill/extra/list.vm";
        } else {
            return "redirect:/error.htm?code=" + bizResult.code;
        }
    }

    @RequestMapping(value = "/edit.htm", method = RequestMethod.GET)
    public String edit(HttpServletRequest request, ModelMap modelMap, ExtraFeeEditForm extraFeeEditForm,
                       BindingResult result, Map<String, Object> model) {
        long id = RequestUtil.getLong(request, "id");
        BizResult bizResult = extraFeeBiz.detail(id);
        if (bizResult.success) {
            modelMap.putAll(bizResult.data);
            ExtraFeeDO extraFeeDO = (ExtraFeeDO) bizResult.data.get("extraFeeDO");
            extraFeeEditForm.initForm(extraFeeDO);

            return "bill/extra/edit.vm";
        } else {
            return "redirect:/error.htm?code=" + bizResult.code;
        }

    }


    @RequestMapping(value = "/create.htm", method = RequestMethod.GET)
    public String create(HttpServletRequest request, ModelMap modelMap, ExtraFeeForm extraFeeForm,
                         BindingResult result) {
        return "bill/extra/create.vm";
    }

    @RequestMapping(value = "/doCreate.htm", method = RequestMethod.POST)
    public String doCreate(HttpServletRequest request, ModelMap modelMap, @Valid ExtraFeeForm extraFeeForm,
                           BindingResult result) {
        if (result.hasErrors()) {

            return "bill/extra/create.vm";
        }


        BizResult bizResult = extraFeeBiz.create(extraFeeForm);
        if (bizResult.success) {
            return "redirect:/bill/extra/list.htm";
        } else {
            return "redirect:/error.htm?code=" + bizResult.code;
        }

    }

    @RequestMapping(value = "/doUpdate.htm", method = RequestMethod.POST)
    public String doUpdate(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap,
                           @Valid ExtraFeeEditForm extraFeeEditForm,
                           BindingResult result, Map<String, Object> model) {
        if (result.hasErrors()) {
            return "bill/extra/edit.vm";
        }
        ExtraFeeDO extraFeeDO = extraFeeEditForm.toDO();
        BizResult bizResult = extraFeeBiz.update(extraFeeDO);
        if (bizResult.success) {
            return "redirect:/bill/extra/list.htm";
        } else {
            return "redirect:/error.htm?code=" + bizResult.code;
        }

    }

    @RequestMapping(value = "/doDelete.htm")
    public void doDelete(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
        long id = RequestUtil.getLong(request, "id");
        BizResult bizResult = extraFeeBiz.delete(id);
        JsonResult result = JsonResult.getInstance();
        if (bizResult.success) {
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        } else {
            result.setErrMsg(ErrorCode.getMessage(bizResult.code));
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        }

    }
}
